﻿@model LanguageSelectorModel
@using SmartStore.Web.Models.Common;
@using SmartStore.Core;
@using SmartStore.Core.Infrastructure;

@if (Model.AvailableLanguages.Count > 1)
{
    var webHelper = EngineContext.Current.Resolve<IWebHelper>();
    var activeLang = Model.AvailableLanguages.Where(x => x.Id == Model.CurrentLanguageId).FirstOrDefault();

    <div class="dropdown language-selector">
        <a class="menubar-link" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" href="#">
			<span>@activeLang.SeoCode.ToUpper()</span>
            <i class="fa fa-angle-down"></i>
        </a>
        
        <div class="dropdown-menu" aria-labelledby="dLabel">
            @foreach (var lang in Model.AvailableLanguages)
            {
                var href = webHelper.ModifyQueryString(Url.RouteUrl("ChangeLanguage", new { langid = lang.Id }), "returnUrl=" + Url.Encode(Model.ReturnUrls[lang.SeoCode]), null);
                
                <a href="@href" title="@lang.NativeName" rel="nofollow" class="dropdown-item@(lang.Id == activeLang.Id ? " disabled" : "")" 
                   data-selected="@(lang.Equals(activeLang) ? "true" : "false")" data-abbreviation="@lang.Name">

                    @if(Model.UseImages)
                    {
                        <span class="mr-1 align-text-bottom">
                            <img src = "@Url.Content("~/Content/images/flags/" + lang.FlagImageFileName)" />
                        </span>
                    }

                    <span>@lang.Name</span>

                    <span class="abbreviation lrm">(@lang.SeoCode.ToUpper())</span>
                </a>
            }
        </div>
    </div>

	<script type="text/javascript">
		$(function () {
			$(".language-selector .dropdown-item.disabled").on('click', function (e) {
				e.preventDefault();
            });
        });
	</script>
}